
# library(cowplot)
# For more on the customized functions, please see this discussion:
# https://github.com/ggobi/ggally/issues/139

my_custom_cor <- function(data, mapping, color = I("grey50"), ...) {
  
  # get the x and y data to use the other code
  x <- GGally::eval_data_col(data, mapping$x)
  y <- GGally::eval_data_col(data, mapping$y)
  
  # Old:
  #x <- eval(mapping$x, data)
  #y <- eval(mapping$y, data)
  
  ct <- cor.test(x,y)
  
  r <- unname(ct$estimate)
  rt <- format(r, digits=2)[1]
  
  # plot the cor value
  ggally_text(
    label = as.character(rt), 
    mapping = aes(),
    xP = 0.5, yP = 0.5, 
    size = 3,
    color = color,
    ...
  ) + 
    # remove all the background stuff and wrap it with a dashed line
    theme_classic() + 
    theme(
      # panel.background = element_rect(
      #   color = color, 
      #   linetype = "longdash"
      # ), 
      axis.line = element_blank(), 
      axis.ticks = element_blank(), 
      axis.text.y = element_blank(), 
      axis.text.x = element_blank()
    )
}


my_custom_smooth <- function(data, mapping, ...) {
  ggplot(data = data, mapping = mapping) +
    geom_jitter(color = '#8da0cb', size = 0.00001) + 
    geom_smooth(method = "loess", color = 'black', lwd = .5, ...) +
    coord_cartesian(ylim = c(0, 1)) +
    theme_minimal() +
    scale_y_continuous(breaks = c(0, .5, 1)) +
    scale_x_continuous(breaks = c(0, .5, 1)) 
}

my_custom_density <- function(data, mapping, ...) {
  ggplot(data = data, mapping = mapping) +
    geom_density(color = '#8da0cb') + 
    theme_minimal() +
    coord_cartesian(ylim = c(0, 8)) +
    scale_x_continuous(breaks = c(0, .5, 1)) +
    scale_y_continuous(breaks = c(0, 4, 8)) 
}

# ggpairs(subset(df, select = c(ling24s, psy24s, soc24s, econ24s, pol24s, nav24s)),
#         lower = list(continuous = my_custom_smooth),
#         diag = list(continuous = my_custom_density),
#         upper = list(continuous = my_custom_cor)
#         )



corMa <- ggpairs(subset(df, select = c(ling24s, psy24s, soc24s, econ24s, pol24s, nav24s)),
                 lower = list(continuous = my_custom_smooth),
                 diag = list(continuous = my_custom_density),
                 upper = list(continuous = my_custom_cor),
                 columnLabels = c("ling24s" = "Ling.", 
                                  "psy24s" = "Psy.",
                                  "soc24s" = "Soc.",
                                  "econ24s" = "Econ.",
                                  "pol24s" = "Pol.",
                                  "nav24s" = "Nav.")) +
  theme(axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6),
        panel.background  = element_blank(),
        panel.border      = element_blank(),
        strip.background  = element_blank(),
        plot.background = element_blank(),
        text = element_text(size = 10)
  )

nrow(na.omit(subset(df, select = c(ling24s, psy24s, soc24s, econ24s, pol24s, nav24s))))

corMaP <- cowplot::plot_grid(
  ggmatrix_gtable(corMa))

ggsave(here::here("Draft/PNAS/draft/plots", "displayItem3.pdf"), 
       corMaP, height = (11.4 * 1), width = 11.4, unit = "cm")


# Same Plot for IPL-12 ----------------------------------------------------

my_custom_density <- function(data, mapping, ...) {
  ggplot(data = data, mapping = mapping) +
    geom_density(color = '#8da0cb') + 
    theme_minimal() +
    coord_cartesian(ylim = c(0, 12)) +
    scale_x_continuous(breaks = c(0, .5, 1)) +
    scale_y_continuous(breaks = c(0, 4, 8, 12)) 
}

corMa12 <- ggpairs(subset(df, select = c(ling12s, psy12s, soc12s, econ12s, pol12s, nav12s)),
                 lower = list(continuous = my_custom_smooth),
                 diag = list(continuous = my_custom_density),
                 upper = list(continuous = my_custom_cor),
                 columnLabels = c("ling24s" = "Ling.", 
                                  "psy24s" = "Psy.",
                                  "soc24s" = "Soc.",
                                  "econ24s" = "Econ.",
                                  "pol24s" = "Pol.",
                                  "nav24s" = "Nav.")) +
  theme(axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6),
        panel.background  = element_blank(),
        panel.border      = element_blank(),
        strip.background  = element_blank(),
        plot.background = element_blank(),
        text = element_text(size = 10)
  )

corMaP12 <- cowplot::plot_grid(
  ggmatrix_gtable(corMa12))

ggsave(here::here("Draft/PNAS/draft/plots", "displayItem3_12.pdf"), 
       corMaP12, height = (11.4 * 1), width = 11.4, unit = "cm")

